package org.exam.blog.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.exam.blog.domain.Comment;
import org.exam.blog.service.ICommentService;
import org.exam.blog.service.impl.CommentServiceImpl;
import org.exam.util.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController
@RequestMapping("/comment")
@Api(tags = "用户提示")
public class CommentController {
    @Autowired
    private ICommentService commentServiceImpl;
    @ApiOperation(value = "查询单个接口")
    @PutMapping
    public JsonResult add(@RequestBody Comment comment) {
        try {
            if (comment.getId() == null) {
                commentServiceImpl.add(comment);
                return JsonResult.success();
            } else {
                commentServiceImpl.update(comment);
                return JsonResult.success();
            }
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.error();

        }
    }
    @DeleteMapping("/{id}")
    public JsonResult delete(@PathVariable String id) {
        try {
            commentServiceImpl.delete(id);
            return JsonResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.error();
        }
    }

    @GetMapping
    public List<Comment> getAll() {
        return commentServiceImpl.getAll();
    }

    @GetMapping("/{articleId}")
    public List<Comment> getComment(@PathVariable("articleId")Long articleId) {
        return commentServiceImpl.findByArticleId(articleId);
    }


}
